//
// Created by wenyaxi on 24-10-29.
//
#include <common.h>

class Solution {
public:
    ListNode *reverseBetween(ListNode *head, int left, int right) {
        ListNode* cur = head;
        int index = 1;
        vector<ListNode*> seg1;
        vector<ListNode*> seg2;
        vector<ListNode*> seg3;
        while (cur) {
            if(index<left) {
                seg1.push_back(cur);
            }else if(index>=left&&index<=right) {
                seg2.push_back(cur);
            }else if(index>right) {
                seg3.push_back(cur);
            }
            cur = cur->next;
            index++;
        }
        ListNode* dummy = new ListNode(0);
        cur = dummy;
        for(int i=0; i<seg1.size(); i++) {
            cur->next = seg1[i];
            cur = cur->next;
        }
        for(int i=seg2.size()-1; i>=0; i--) {
            if(i!=0) {
                cur->next = seg2[i];
                cur = cur->next;
            }else {
                cur->next = seg2[i];
                cur = cur->next;
                cur->next=nullptr;
            }
        }
        for(int i=0;i<seg3.size();i++) {
            cur->next = seg3[i];
            cur = cur->next;
        }
        return dummy->next;
    }
};

int main(int argc, char *argv[]) {
    auto list_node = ListNode::of({3,5});
    Solution s;
    auto reverse_between = s.reverseBetween(list_node, 1, 2);
    cout<<*reverse_between<<endl;
    return 0;
}
